import { post,get,getP } from '@/api/customedAxios'
import { getToken, setToken, removeToken } from '@/utils/auth'

const getDefaultState = () => {
    return {
        data: '',
    }
}

const state = getDefaultState()


const mutations = {
    SET_DATA: (state, data) => {
        state.data = data
    }
}

const actions = {
    post({ commit }, requestBody) {
        // return new Promise((resolve, reject) => {
        return new Promise((resolve, reject) => {
            post(requestBody.url, requestBody).then(response => {
                commit("SET_DATA", response.data)
                resolve(response.data)
                // let stateCode = response.headers["state"]
                // if (stateCode == "200") {
                //     commit("SET_DATA", response.data)
                // }
                // else if (stateCode == null) {
                //     this.$message.success("请求异常！")
                // }
                // else {
                //     this.$message.error(this.$enum.getDescByValue('STATE', stateCode))
                // }
            })
            // .catch(function (error) {
            //     let stateInfo = error.response.headers["state"];
            //     if (stateInfo != "") {
            //         _this.$message.error(
            //             _this.$enum.getDescByValue("STATE", stateInfo)
            //         );
            //     }
            //     else{

            //     }
            // })
            // .then(function () {
            //     console.log("测试成功");
            // });
        }).catch(error => {
            reject(error)
        })
    },
    get({ commit }, url) {
        // return new Promise((resolve, reject) => {
        return new Promise((resolve, reject) => {
            get(url).then(response => {
                commit("SET_DATA", response.data)
                resolve(response.data)
            })
        }).catch(error => {
            reject(error)
        })
    },
    getP({ commit }, data) {
        // return new Promise((resolve, reject) => {
        return new Promise((resolve, reject) => {
            getP(data.url,data).then(response => {
                commit("SET_DATA", response.data)
                resolve(response.data)
            })
        }).catch(error => {
            reject(error)
        })
    },
    postF({ commit }, requestBody) {
        return new Promise((resolve, reject) => {
            post(requestBody.get("url"), requestBody).then(response => {
                commit("SET_DATA", response.data)
                resolve(response.data)
            })
        }).catch(error => {
            reject(error)
        })
    },
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}

